﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lm.Common.ExtensionMethods;
using Lm.Common.Auth.Operation;

namespace Lm.Common.Auth.Role
{
    [Operation.Operation("{04A20F6B-4517-42F1-A663-372102522F98}", "正式角色管理员", "")]
    public class RoleAdmin : Operation.OperationBase
    {
        static RoleAdmin()
        { Operation.OperationBase.CreateOperation(typeof(RoleAdmin)); }

        public RoleAdmin(string token)
            : base(token)
        { }

        public void ConfirmRole(string roleCode)
        {
            using (var tran = new System.Transactions.TransactionScope())
            {
                var role = new RoleDal().GetByRoleCode(roleCode);
                if (role.GetRoleStatus() != Model.RoleStatus.Draft)
                { throw new Exception("只能审核草稿状态下的角色"); }
                var helper = Lm.Common.Auth.DAL.DALFactory.Create();

                role.SetRoleStatus(Model.RoleStatus.Live);
                if (helper.Update<Model.Role>(role, t => t.RoleId == role.RoleId) != 1)
                { throw new Exception("审核角色失败"); }
                tran.Complete();
            }
        }

        public void KillRole(string roleCode)
        {
            using (var tran = new System.Transactions.TransactionScope())
            {
                var role = new RoleDal().GetByRoleCode(roleCode);
                if (role.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("只能作废可用状态下的角色"); }
                var helper = Lm.Common.Auth.DAL.DALFactory.Create();

                role.SetRoleStatus(Model.RoleStatus.Death);
                if (helper.Update<Model.Role>(role, t => t.RoleId == role.RoleId) != 1)
                { throw new Exception("作废角色失败"); }
                tran.Complete();
            }
        }

        public void EditRole(Model.Role role)
        {
            role.TrimStringProperty();
            if (role.RoleCode.IsNullOrBlank())
            { throw new Exception("角色编号必填"); }
            if (role.RoleName.IsNullOrBlank())
            { throw new Exception("角色名必填"); }

            using (var tran = new System.Transactions.TransactionScope())
            {
                var data = new RoleDal().GetByRoleCode(role.RoleCode);
                if (data.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("角色不是可用状态，不能修改"); }
                data.Pause = role.Pause;
                data.RoleName = role.RoleName;
                data.RoleRemark = role.RoleRemark;
                if (DAL.DALFactory.Create().Update<Model.Role>(data, t => t.RoleId == data.RoleId) != 1)
                { throw new Exception("修改角色失败"); }
                tran.Complete();
            }
        }

        /// <summary>
        /// 给角色授权
        /// </summary>
        /// <param name="roleCode"></param>
        /// <param name="operationIdList"></param>
        public void Authorize(string roleCode, params Guid[] operationIdList)
        {
            if (roleCode.IsNullOrBlank())
            { throw new Exception("角色编号不能为空"); }
            if (operationIdList.Length == 0)
            { throw new Exception("权限列表不能为空"); }
            using (var tran = new System.Transactions.TransactionScope())
            {
                var role = new RoleDal().GetByRoleCode(roleCode);
                if (role.GetRoleStatus() != Model.RoleStatus.Live)
                { throw new Exception("角色不是可用状态，不能授权"); }
                var helper = DAL.DALFactory.Create();
                helper.Delete<Model.RolePermission>(t => t.RoleId == role.RoleId);
                foreach (var operationId in operationIdList)
                {
                    var operation = new OperationDal().GetById(operationId);
                    if (helper.Insert<Model.RolePermission>(new Model.RolePermission()
                    {
                        Id = Guid.NewGuid(),
                        RoleId = role.RoleId,
                        OperationId = operationId
                    }) == false)
                    { throw new Exception("授权失败"); }
                }
                tran.Complete();
            }
        }

    }
}
